define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/labor/index' + location.search,
                    add_url: 'pt/labor/add',
                    edit_url: 'pt/labor/edit',
                    // del_url: 'pt/labor/del',
                    multi_url: 'pt/labor/multi',
                    import_url: 'pt/labor/import',
                    table: 'pt_labor',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'admin_str', title: __('劳务公司系统管理员'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        statistic: function () {
            queryDate = $("#query_date").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/labor/statistic?query_date=' + queryDate,
                    add_url: '',
                    edit_url: '',
                    // del_url: 'pt/labor/del',
                    multi_url: '',
                    import_url: '',
                    table: 'pt_labor',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                escape: false,
                pagination: false,
                columns: [
                    [
                        {field: 'labor_name', title: __('劳务公司'), operate: false},
                        {field: 'add_total', title: __('新增人次'), operate: false},
                        {field: 'del_total', title: __('删除人次'), operate: false},
                        {field: 'absent_total', title: __('缺岗人次'), operate: false},
                        {field: 'del_percent', title: __('删除比例'), operate: false, 
                          formatter: function(value, row, index) {
                            return value + '%';
                          }
                        },
                        {field: 'absent_percent', title: __('缺岗比例'), operate: false, 
                          formatter: function(value, row, index) {
                            return value + '%';
                          }
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {   
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {   
                                  name: 'ajax',
                                  text: __('导出'),
                                  icon: 'fa',
                                  classname: 'btn btn-xs btn-info btn-click',
                                  click: function (data, row) {
                                      var queryDate = $("#query_date").val();
                                      var laborId = row.id; 
                                      Fast.api.ajax({
                                          url:'pt/labor/export?queryDate=' + queryDate + '&labor_id='  + laborId
                                      }, function(res){
                                          console.log('export =>', res)
                                          window.location.href=res['file']
                                      }, function(){
                                          console.log('导出失败');
                                          Backend.api.toastr.error('导出失败');
                                      });
                                  }
                              },  
                          ],  

                          formatter: Table.api.formatter.operate

                        } 
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(".btn-embossed").on("click", function() {
                queryDate = $("#query_date").val();
                table.bootstrapTable('refresh', {
                  url: 'pt/labor/statistic?query_date=' + queryDate
                })
            })
            Form.api.bindevent($("form[role=form]"));
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
